﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EmbeddedEntitySchemaItem.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.Schema
{
  using System;
  using System.Collections.Generic;

  using Lextm.SharpSnmpLib;

  /// <summary>
  /// The embedded entity schema item.
  /// </summary>
  public class EmbeddedEntitySchemaItem : ISchemaItem
  {
    #region Fields

    /// <summary>
    /// The entity.
    /// </summary>
    private readonly IDefinition entity;

    /// <summary>
    /// The parent.
    /// </summary>
    private readonly ISchemaItem parent;

    #endregion

    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="EmbeddedEntitySchemaItem"/> class.
    /// </summary>
    /// <param name="entity">
    /// The entity.
    /// </param>
    /// <param name="parent">
    /// The parent.
    /// </param>
    /// <param name="isRootItem">
    /// The is root item.
    /// </param>
    internal EmbeddedEntitySchemaItem(IDefinition entity, ISchemaItem parent, bool isRootItem)
    {
      this.entity = entity;
      this.parent = parent;

      if (!isRootItem)
      {
        this.NameSpace = string.IsNullOrEmpty(parent.NameSpace) ? parent.Name : parent.NameSpace + "." + parent.Name;
        uint[] id = entity.GetNumericalForm();

        if (1 == id.Length)
        {
          this.Oid = new ObjectIdentifier(new uint[] { 0, id[0] });

          // TODO: FixMe, this is not correct, just to make it run...
        }
        else
        {
          this.Oid = new ObjectIdentifier(entity.GetNumericalForm());
        }
      }
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets or sets the children.
    /// </summary>
    public IEnumerable<ISchemaItem> Children { get; set; }

    /// <summary>
    /// Gets a value indicating whether is columnar.
    /// </summary>
    /// <exception cref="NotImplementedException">Always throws this exception.</exception>
    public bool IsColumnar
    {
      get
      {
        throw new NotImplementedException();
      }
    }

    /// <summary>
    /// Gets a value indicating whether is scalar.
    /// </summary>
    /// <exception cref="NotImplementedException">Always throws this exception.</exception>
    public bool IsScalar
    {
      get
      {
        throw new NotImplementedException();
      }
    }

    /// <summary>
    /// Validates the schema item.
    /// </summary>
    /// <param name="knownConstructs">
    /// The known constructs.
    /// </param>
    /// <param name="fileName">
    /// The file name.
    /// </param>
    /// <returns>
    /// If valid, true is returned otherwise false.
    /// </returns>
    public bool Validate(List<IConstruct> knownConstructs, string fileName)
    {
      return this.entity.Validate(knownConstructs, fileName);
    }

    /// <summary>
    /// Gets or sets the module name.
    /// </summary>
    public string ModuleName
    {
      get
      {
        return this.entity.ModuleName;
      }

      set
      {
        this.entity.ModuleName = value;
      }
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    public string Name
    {
      get
      {
        return this.entity.Name;
      }

      set
      {
        this.entity.Name = value;
      }
    }

    /// <summary>
    /// Gets the name space.
    /// </summary>
    public string NameSpace { get; internal set; }

    /// <summary>
    /// Gets or sets the oid.
    /// </summary>
    public ObjectIdentifier Oid { get; set; }

    /// <summary>
    /// Gets or sets the parent.
    /// </summary>
    public string Parent
    {
      get
      {
        return this.entity.Parent;
      }

      set
      {
        this.entity.Parent = value;
      }
    }

    /// <summary>
    /// Gets the parent item.
    /// </summary>
    public ISchemaItem ParentItem
    {
      get
      {
        return this.parent;
      }
    }

    /// <summary>
    /// Gets the type.
    /// </summary>
    public DefinitionType Type
    {
      get
      {
        return this.entity.Type;
      }
    }

    /// <summary>
    /// Gets or sets the value.
    /// </summary>
    public uint Value
    {
      get
      {
        return this.entity.Value;
      }

      set
      {
        this.entity.Value = value;
      }
    }

    #endregion
  }
}